package git.soulbgm.security.handler;

import git.soulbgm.security.pojo.LoginUser;
import git.soulbgm.security.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义注销处理类
 *
 * @author SoulBGM
 * @date 2020-08-03
 */
@Slf4j
@Component
public class LogoutHandlerImpl implements LogoutHandler {

    @Autowired
    private TokenService tokenService;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        if (authentication != null) {
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            tokenService.delLoginUser(loginUser.getUsername(), loginUser.getToken());
            //request.getSession().getId()
            //log.info("{},正常退出",loginUser.getNickname());
        }
    }
}
